package com.zhj.micro.middleware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class OrderConfiguration {

    public static final String ORDER_QUEUE_NAME = "order_queue";

    public static final String ORDER_EXCHANGE_NAME = "order_exchange";

    public static final String ORDER_ROUTINGKEY = "order.create";

    @Bean
    public Queue orderQueue() {
        //绑定死信队列，订单超时未被消费，则进入死信队列
        return QueueBuilder.durable(ORDER_QUEUE_NAME).withArgument("x-dead-letter-exchange", DLX_EXCHANGE).build();
    }


    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(ORDER_EXCHANGE_NAME);
    }

    /**
     * 创建交换机进行绑定
     */
    @Bean
    public Binding bindingOrder() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_ROUTINGKEY);
    }


    /**
     * -------------------------------------------------------------- 创建库存扣减成功后订单状态修改队列
     * @return
     */

    public static final String ORDER_PRODUCT_QUEUE_NAME = "order_product_queue";

    public static final String ORDER_PRODUCT_EXCHANGE_NAME = "order_product_exchange";

    public static final String ORDER_PRODUCT_ROUTINGKEY = "order.product";

    @Bean
    public Queue orderProductQueue() {
        //绑定死信队列，订单超时未被消费，则进入死信队列
        return QueueBuilder.durable(ORDER_PRODUCT_QUEUE_NAME).withArgument("x-dead-letter-exchange", DLX_EXCHANGE).build();
    }


    @Bean
    public DirectExchange orderProductExchange() {
        return new DirectExchange(ORDER_PRODUCT_EXCHANGE_NAME);
    }

    /**
     * 创建交换机进行绑定
     */
    @Bean
    public Binding bindingProductOrder() {
        return BindingBuilder.bind(orderProductQueue()).to(orderProductExchange()).with(ORDER_PRODUCT_ROUTINGKEY);
    }


    /**
     * -------------------------------------------------------------- 创建商品库存队列
     * @return
     */
    public static final String PRODUCT_QUEUE_NAME = "product_queue";

    public static final String PRODUCT_EXCHANGE_NAME = "product_exchange";

    public static final String PRODUCT_ROUTINGKEY = "product.create";

    @Bean
    public Queue productQueue() {
        return QueueBuilder.durable(PRODUCT_QUEUE_NAME).build();
    }


    @Bean
    public DirectExchange productExchange() {
        return new DirectExchange(PRODUCT_EXCHANGE_NAME);
    }

    /**
     * 创建交换机进行绑定
     */
    @Bean
    public Binding bindingProduct() {
        return BindingBuilder.bind(productQueue()).to(productExchange()).with(PRODUCT_ROUTINGKEY);
    }


    /**
     * -------------------------------------------------------------- 创建死信队列
     * @return
     */
    public static final String DLX_EXCHANGE = "dlx_exchange";

    public static final String DLX_QUEUE = "dlx_queue";

    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable(DLX_QUEUE).build();
    }

    @Bean
    public TopicExchange dlxExchange() {
        return new TopicExchange(DLX_EXCHANGE);
    }

    @Bean
    public Binding bindingToDlx() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("dlx.order.*");
    }


    /**
     * -------------------------------------------------------------- 创建延时队列：主要用于订单超时未支付的情况
     * @return
     */

    public static final String DELAY_ORDER_EXCHANGE = "delay_order_exchange";

    public static final String DELAY_ORDER_QUEUE = "delay_order_queue";

    public static final String DELAY_ORDER_ROUTINGKEY = "delay.order.*";

    @Bean
    public Exchange delayExchange() {
        Map<String, Object> map = new HashMap<>();
        //指定路由类型为直接交换机
        map.put("x-delayed-type", "topic");
        // 第二个交换机的类型自定义设置为 x-delayed-message 延迟队列指定
        return new CustomExchange(DELAY_ORDER_EXCHANGE, "x-delayed-message", true, false, map);
    }

    @Bean
    public Queue delayQueue() {
        return QueueBuilder
            .durable(DELAY_ORDER_QUEUE)
            .build();
    }

    @Bean
    public Binding bindingToDelay() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(DELAY_ORDER_ROUTINGKEY).noargs();
    }

}
